<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>华为交换机 MAC-IP 查询</title>
    <link rel="stylesheet" href="/static/style.css">
</head>
<body>
    <div class="container">
        <h1>🔧 华为交换机 MAC-IP 绑定查询</h1>
                <!-- ★★ 交换机 IP 已隐藏 ★★ -->
        <form id="connectForm">
            <!-- 前端保留隐藏域，后台仍按原字段接收 -->
            <input type="hidden" id="host" value="192.168.10.254">

{#       <form id="connectForm">#}
{#    <label>交换机 IP：</label>#}
{#    <input type="text" id="host" value="192.168.10.254" required>#}

{#    <label>SSH 端口：</label>#}
{#    <input type="number" id="port" value="2222" required>#}

{#    <label>用户名：</label>#}
{#    <input type="text" id="username" required>#}
{##}
{#    <label>密码：</label>#}
{#    <input type="password" id="password" required>#}

    <!-- ★★★ 新增网段下拉框 ★★★ -->
    <label>选择要查询的网段：</label>
    <select id="subnet" required>
        <option value="all">全部网段</option>   <!-- 置顶，默认 -->
        <option value="192.168.10">10网段</option>
        <option value="192.168.12">12网段</option>
        <option value="192.168.15">15网段</option>
        <option value="192.168.20">20网段</option>
    </select>

    <button type="submit">连接并获取数据</button>
</form>

        <div id="loading" style="display:none;">🔄 正在连接，请稍候...</div>
        <div id="error" class="error"></div>
        <div id="success" class="success"></div>

        <table id="resultTable" style="display:none;">
            <thead>
                <tr>
                    <th>IP 地址</th>
                    <th>MAC 地址</th>
                    <th>接口</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>

    <script>
        document.getElementById('connectForm').addEventListener('submit', async function (e) {
            e.preventDefault();

            const data = {
                host: document.getElementById('host').value,
                {#port: document.getElementById('port').value,#}
                {#username: document.getElementById('username').value,#}
                {#password: document.getElementById('password').value,#}
                subnet: document.getElementById('subnet').value
            };

            document.getElementById('loading').style.display = 'block';
            document.getElementById('error').style.display = 'none';
            document.getElementById('success').style.display = 'none';
            document.getElementById('resultTable').style.display = 'none';

            try {
                const res = await fetch('/api/get_mac_bindings', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(data)
                });

                const result = await res.json();
                document.getElementById('loading').style.display = 'none';

                if (result.success) {
                    document.getElementById('success').textContent = `✅ 获取到 ${result.data.length} 条静态绑定记录`;
                    document.getElementById('success').style.display = 'block';
                    displayTable(result.data);
                } else {
                    document.getElementById('error').textContent = '❌ ' + result.error;
                    document.getElementById('error').style.display = 'block';
                }
            } catch (err) {
                document.getElementById('loading').style.display = 'none';
                document.getElementById('error').textContent = '❌ 网络错误或服务器无响应';
                document.getElementById('error').style.display = 'block';
            }
        });

        function displayTable(data) {
            const tbody = document.querySelector('#resultTable tbody');
            tbody.innerHTML = '';
            data.forEach(item => {
                const tr = document.createElement('tr');
                tr.innerHTML = `
                    <td>${item.ip_address}</td>
                    <td>${item.mac_address}</td>
                    <td>${item.interface}</td>
                `;
                tbody.appendChild(tr);
            });
            document.getElementById('resultTable').style.display = 'table';
        }
    </script>
</body>
</html>